package com.medical.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.system.exception.MyCustomerException;
import com.medical.system.mapper.SysMenuMapper;
import com.medical.system.mapper.SysRoleMenuMapper;
import com.medical.system.model.SysMenu;
import com.medical.system.model.SysRoleMenu;
import com.medical.system.service.SysMenuService;
import com.medical.system.util.MenuHelper;
import com.medical.system.util.RouterHelper;
import com.medical.system.vo.AssginMenuVo;
import com.medical.system.vo.RouterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    //获取所有的菜单
    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> menuList = this.baseMapper.selectList(null);
        menuList.stream().forEach(System.out::println);
        List<SysMenu> sysMenus = MenuHelper.bulidTree(menuList);
        return sysMenus;
    }

    //根据id = parentid，查询当前删除菜单下面是否子菜单
    @Override
    public void removeMenuById(Long id) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", id);
        Integer count = baseMapper.selectCount(wrapper);
        if (count > 0) {
            throw new MyCustomerException(201, "请先删除子菜单");
        }
        baseMapper.deleteById(id);
    }

    //   根据角色ID获取权限树
    @Override
    public List<SysMenu> findSysMenuByRoleId(Long roleId) {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<SysMenu> menuList = baseMapper.selectList(queryWrapper);
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        List<SysRoleMenu> roleMenus = sysRoleMenuMapper.selectList(wrapper);
        List<Long> roleMenuIds = new ArrayList<>();
        for (SysRoleMenu roleMenu : roleMenus) {
            roleMenuIds.add(roleMenu.getMenuId());
        }
        for (SysMenu sysMenu : menuList) {
            if (roleMenuIds.contains(sysMenu.getId())) {
                sysMenu.setSelect(true);
            } else {
                sysMenu.setSelect(false);
            }
        }
        List<SysMenu> sysMenus = MenuHelper.bulidTree(menuList);
        return sysMenus;
    }

    //    分配新权限替换旧权限
    @Override
    public void doAssign(AssginMenuVo assginMenuVo) {
        sysRoleMenuMapper.delete(new QueryWrapper<SysRoleMenu>().eq("role_id", assginMenuVo.getRoleId()));
        for (Long menuId : assginMenuVo.getMenuIdList()) {
            if (menuId != null) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setMenuId(menuId);
                sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
                sysRoleMenuMapper.insert(sysRoleMenu);
            }
        }
    }

    //    根据ID获取用户路由权限
    @Override
    public List<RouterVo> findUserMenuList(Long userId) {
        //超级管理员admin账号id为：1
        List<SysMenu> sysMenuList = null;
        if (userId.longValue() == 1) {
            //a.  表示是超级管理员
            sysMenuList = baseMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 1).orderByAsc("sort_value"));
        } else {
            // b. 其他非超级管理员的 用户
            sysMenuList = baseMapper.findListByUserId(userId);
        }
        List<SysMenu> sysMenuTreeList = MenuHelper.bulidTree(sysMenuList);
        //d.构建路由
        List<RouterVo> routerVoList = RouterHelper.buildRouters(sysMenuTreeList);
        return routerVoList;
    }

    //    根据ID获取用户的权限标识
    @Override
    public List<String> findUserPermsList(Long id) {
        //超级管理员admin账号id为：1
        List<SysMenu> sysMenuList = null;
        if (id.longValue() == 1) {
            sysMenuList = this.baseMapper.selectList(new QueryWrapper<SysMenu>().eq("status", 1));
        } else {
            sysMenuList = this.baseMapper.findListByUserId(id);
        }
        List<String> permissionList = new ArrayList<>();
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getType() == 2) {
                permissionList.add(sysMenu.getPerms());
            }
        }
        return permissionList;
    }
}
